<template>
    <div class="back_nav overflow" v-show="back || beginTimer">
        <div class="back_box fl" @click="handleBack" v-show="back">
            <img src="@/assets/img/back_icon.png" alt="">
            <span class="middle col_blue font_34">返回</span>
        </div>
        <div class="time_box fr" v-show="beginTimer">
            <img src="../assets/img/time_icon.png" alt="">
            <span class="col_666 font_30">({{time}}s)</span>
        </div>
    </div>
</template>

<script>
  export default {
    name: 'top-timer',
    props: {
      back: {
        type: Boolean,
        default: true,
      },
      pathName: {
        type: String,
        default: '',
      },
      second: {
        type: Number,
        default: 240,
      },
      beginTimer: {
        type: Boolean,
        default: true,
      },
      isAdmin:{
        type: Boolean,
        default: true,
      }
    },
    data () {
      return {
        time: this.second,
        timer: Object,
      }
    },
    destroyed () {
      clearInterval(this.timer)
    },
    created () {
      this.initTimer()
    },
    methods: {
      handleBack () {
        if (this.pathName) {
          if(this.pathName ==='Home'){
            this.$router.push({path: 'Home', query: {isAdmin: true}});
          } else {
            this.$router.push(this.pathName)
          }
          return
        }
        this.$router.back()
      },
      initTimer () {
        clearInterval(this.timer)
        this.time = this.second
        if (this.beginTimer) {
          this.timer = setInterval(() => {
            this.time--
            if (this.time <= 0) {
              clearInterval(this.timer)
              this.$emit('over')
              localStorage.removeItem('token')
              this.$router.push('Home')
            }
          }, 1000)
        }
      },
    },
  }
</script>

<style lang="scss">
    .back_nav {
        height: 1.2rem;
        padding-top: 0.3rem;
        box-sizing: border-box;

        .back_box {
            padding: 0 0.20rem;
            background-color: #D8EBFF;
            width: 1.68rem;
            height: 0.68rem;
            line-height: 0.48rem;
            border-radius: 0 2.00rem 2.00rem 0;

            span {
                display: inline-block;
            }

            img {
                margin-right: 0.10rem;
                width: 0.4rem;
                height: 0.4rem;
            }
        }

        .time_box {
            height: 0.68rem;
            padding-right: 0.2rem;

            img {
                width: 0.4rem;
                height: 0.4rem;
                vertical-align: top;
                position: relative;
                top: 0.06rem;
                right: 0.1rem;
            }

            span {
                vertical-align: top;
            }
        }
    }
</style>